/*  Game/Army Editor for yALB - Yet Another List Builder (roster Editor for Wargames/tabletop games).
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#include "globaltextwidget.h"
#include "ui_globaltextwidget.h"

#include "other/globaltextmodel.h"
#include "collections/globaltextcollection.h"
#include "other/modelcommon.h"

#include <QMessageBox>

GlobalTextWidget::GlobalTextWidget(GlobalTextCollection *globalTexts, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GlobalTextWidget)
{
    ui->setupUi(this);

    Q_ASSERT(globalTexts);

    _globalTexts = globalTexts;

    // \NOTE: GlobalTextModel is constructed here
    _textModel = new GlobalTextModel(_globalTexts, this);

    ui->globalTextsGrid->setModel(_textModel);

    connect(ui->globalTextsGrid->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
            this, SLOT(currentItemChanged(QModelIndex,QModelIndex)));
}

GlobalTextWidget::~GlobalTextWidget()
{
    delete ui;
}

void GlobalTextWidget::clearGlobalTextForm() {
    ui->textKey->setText("");
}

void GlobalTextWidget::currentItemChanged(const QModelIndex &current, const QModelIndex &previous)
{
    Q_UNUSED(previous)
    if(current.isValid()) {
        ui->textKey->setText(current.data(ModelCommon::KeyRole).toString());
        ui->updateTextKeyButton->setEnabled(true);
        ui->deleteTextButton->setEnabled(true);
    } else {
        ui->updateTextKeyButton->setEnabled(false);
        ui->deleteTextButton->setEnabled(false);
    }
}

void GlobalTextWidget::on_addTextButton_clicked()
{
    _globalTexts->add(GlobalText(), ui->textKey->text());
}

void GlobalTextWidget::on_updateTextKeyButton_clicked()
{
    QModelIndex current = ui->globalTextsGrid->currentIndex();
    Q_ASSERT(current.isValid());
    QString key = current.data(ModelCommon::KeyRole).toString();
    QString newKey = ui->textKey->text();
    _globalTexts->update(key, _globalTexts->get(key), newKey);
}

void GlobalTextWidget::on_deleteTextButton_clicked()
{
    QModelIndex current = ui->globalTextsGrid->currentIndex();
    Q_ASSERT(current.isValid());
    if(QMessageBox::warning(0, tr("Are you sure?"), tr("Deleting a global text will make invalid all values that use it. Are you sure you want to continue?"),
                            QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
        _globalTexts->remove(current.data(ModelCommon::KeyRole).toString());
    }
}

void GlobalTextWidget::changeEvent(QEvent *event) {
    if(event->type() == QEvent::LanguageChange)
        ui->retranslateUi(this);
    QWidget::changeEvent(event);
}
